1. Field of Invention
The present invention relates generally to methods and apparatus for improving the performance of software applications. More particularly, the present invention relates to methods and apparatus for processing exceptions as a part of the normal control flow of a computer program.
2. Description of the Related Art
In an effort to increase the efficiency associated with the execution of computer programs, many computer programs are xe2x80x9coptimized.xe2x80x9d Optimizing a computer program generally serves to eliminate portions of computer code which are essentially unused. In addition, optimizing a computer program may restructure computational operations to allow overall computations to be performed more efficiently, thereby consuming fewer computer resources.
An optimizer is arranged to effectively transform a computer program, e.g., a computer program written in a programming language such as C++, FORTRAN or Java bytecodes into a faster program. The faster, or optimized, program generally includes substantially all the same, observable behaviors as the original, or preconverted, computer program. Specifically, the optimized program includes the same mathematical behavior has its associated original program. However, the optimized program generally recreates the same mathematical behavior with fewer computations.
Optimizers are generally arranged to handle exceptions associated with a computer program. As will be appreciated by those skilled in the art, an exception is typically an event or a condition that requires processing outside of the normal control flow. As the name xe2x80x9cexceptionxe2x80x9d implies, it is intended to signal rare, or exceptional, events. While it is possible for programs to use exceptions in their normal handling, the handling of exceptions is usually associated with rare error conditions. Exceptions associated with computer programs typically either involve array range checks or invalid pointer accesses.
A range check is implemented in a computer program to ascertain whether a reference to an array stays within the bounds of the array. Specialized code may be included in a computer program to handle a range check. When a reference to an array does not stay within the bounds of the array, an exception may be raised to indicate that the reference is out of the bounds of the array.
An invalid pointer reference generally involves using a pointer that does not point to any valid memory location. In some programming languages, e.g., the Java(trademark) programming language developed by Sun Microsystems, Inc. of Palo Alto, California, a pointer either points to a valid memory location or is the null value. When a null pointer is referenced, an exception is thrown. In some cases, redundant tests for null pointers, i.e., xe2x80x9cnull checks,xe2x80x9d may occur. When a first test indicates that a pointer is not the null value, a second test that is arranged to test whether a pointer is the null value is a redundant test. As will be appreciated by those skilled in the art, a test for a null pointer is implicitly performed when an attempt is made to load the contents associated with a pointer.
FIG. 1a is a diagrammatic representation of a source code which includes commands to load pointers and, hence, implicitly includes null checks. Source code 102 includes commands, or instructions, to load contents associated with pointers into registers. By way of example, a command 103 is a command to load the contents of a field xe2x80x9cAxe2x80x9d through pointer xe2x80x9cptrl.xe2x80x9d When such a command occurs, the central processing unit [CPU] hardware makes a check to determine if the pointer is a valid pointer. For the Java(trademark) programming language, the only invalid pointer is the null pointer. Hence, the check by the CPU hardware check is essentially a check for the null pointer. A command 104 is also a command to load through pointer xe2x80x9cptrl.xe2x80x9d Although a determination has generally already made regarding whether pointer xe2x80x9cptrlxe2x80x9d is null with respect to command 103, an essentially redundant check regarding whether pointer xe2x80x9cptrlxe2x80x9d is generally made with respect to command 104. Source code 102 is provided to an optimizer 106 which is arranged to optimize source code 102. Once an optimization process is completed, then machine code 110 associated with source code 102 is created.
When an exception is thrown in response to the identification of a null pointer, a table is typically accessed to process the exception. Such a table is often a look-up table that maps a type of exception to a suitable code segment for handling the exception. FIG. 1b is a diagrammatic representation of code which handles exceptions and accesses a look-up table to process the exceptions. A code representation 152 includes calls arranged to throw different types of exceptions. A call 154 to a function that throws a xe2x80x9cTrueExceptionxe2x80x9d results in a look-up table 160 being accessed. Specifically, the xe2x80x9cTrueExceptionxe2x80x9d is used as an index into look-up table 160 such that an appropriate segment of code associated with the throwing of a xe2x80x9cTrueException.xe2x80x9d As shown, a segment of code 157 is eventually accessed by call 154. Similarly, a call 156 to a function that throws a xe2x80x9cFalseExceptionxe2x80x9d maps to a segment of code 158 through look-up table 160.
In general, each time an exception is thrown, e.g., each time a call is made to a function that throws a xe2x80x9cTrueException,xe2x80x9d storage is allocated to an exception object. That is, an exception object is created for each exception that is thrown. Such exception objects are often never used, as will be appreciated by those skilled in the art. However, exception objects are considered to be in use and, hence, are not eliminated by an optimizer during an optimization process. Therefore, the time to initialize exception objects and the memory space occupied by exception objects is effectively wasted as the memory space may not be reallocated for alternative uses.
In a programming language such as the Java(trademark) programming language, precise exception handling is typically required. Precise exception handling generally requires that exception checks be neither reordered nor removed. As such, redundant code is often associated with the handling of an exception such as a null pointer exception, as previously mentioned.
Although specific exception handling code may be relatively effective in handling exceptions, since the code that handles exceptions is often redundant, the optimization of source code which includes exception handling code may not be complete. In other words, an optimization process may not produce a program that is optimized as much as possible. When a program is not completely optimized, the program may not execute efficiently. Further, as previously mentioned, each time an exception is thrown, storage is allocated to an exception object. Since exception objects are generally not used, redundant exception objects created from redundant exception code increase the amount of wasted memory space associated with a computer system.
In order to further optimize programs with precise exceptions, optimizers typically include special optimization techniques to handle the exceptions. The presence of these special techniques requires custom code to be added to the optimizer. This custom code is a often a source of subtle bugs. Further, such custom code is often not well integrated into the rest of the optimizer, and will not be able to remove some exception checks that could be removed if the full power of the optimizer could be brought to bear.
Therefore, what is desired is an efficient method for handling exceptions. Specifically, what is needed is a method and an apparatus for substantially eliminating redundant exception checks by enabling an optimizer to handle exceptions as part of normal program control flow.
The present invention relates to handling exceptions as a part of normal program control flow. According to one aspect of the present invention, a method for processing an exception in an object-based system includes obtaining source code that includes a code segment which is arranged to explicitly specify instructions pertaining to an exception check. The method also includes creating a final internal representation of the source code at least in part by removing a call associated with the exception check, and generating machine code associated with the final internal representation. In one embodiment, the method further includes executing the machine code.
In another embodiment, creating the final internal representation of the source code includes processing the exception check as a part of a control flow associated with the source code. In such an embodiment, creating the final internal representation of the source code further includes optimizing the processing of the exception check.
By integrating the handling of exceptions into normal program control flow, the efficiency of handling exceptions may be increased. For instance, exceptions which are handled as part of normal program control flow may have redundant checks eliminated by the mechanism which removes redundant checks in normal control flow. Additionally, exception objects which are never used may be eliminated when exceptions are handled as part of normal program control flow, i.e., the optimizer already removes unused code and with this invention may also remove code associated with exceptions, thereby freeing resources associated with the exception objects.
According to another aspect of the present invention, a method for creating a representation of source code with code portions associated with exceptions includes first obtaining the source code. The source code includes a first code portion that is nested within a second code portion. A determination is made as to whether the first code portion is redundant with respect to the second code portion, and a transformation is performed to change the source code into an internal representation. Such a transformation of the source code into an internal representation includes eliminating the first code portion from the source code when it is determined that the first code portion is redundant with respect to the second code portion. In one embodiment, the internal representation is further transformed into machine code.
In another embodiment, each of the first code portion and the second code portion includes a determination of whether to throw a particular exception. In such an embodiment, determining whether the first code portion is redundant with respect to the second code portion includes determining if the first code portion is arranged to be reached by a program control flow associated with the source code when the particular exception is not to be thrown.
These and other advantages of the present invention will become apparent upon reading the following detailed descriptions and studying the various figures of the drawings.